//package
package uit.server.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import uit.upis.model.Layer;

public class LayerUtil {
/*	
	private static HashMap checkLayer(HashMap theme, MapInfo mapInfo) {
		HashMap layers = new HashMap();
//		log.debug("Current Scale="+mapInfo.getScale());
		
		try {
			for (int i=0;i<=theme.size();i++) {
				LayerInfo info = (LayerInfo)theme.get(String.valueOf(i));
				if ( info != null ) {
//					log.debug(i+" "+info.getId()+" "+info.isVisible()+" maxScale="+info.getMaxScale()+" minScale="+info.getMinScale());
					if ( info.getLevel() == 1 )
						layers.put(String.valueOf(info.getId()), info);
					else if ( info.isVisible() && mapInfo.getScale() < info.getMaxScale() && mapInfo.getScale() > info.getMinScale() ) {
						LayerInfo temp = (LayerInfo)layers.get(String.valueOf(info.getPreLayer()));
						if ( temp != null && temp.isVisible() ) {
							layers.put(String.valueOf(info.getId()), info);
						}
					}
				}
			}
			if ( !layers.containsKey("69") )  layers.remove("60");
			if ( !layers.containsKey("103") ) layers.remove("61");
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return layers;
	}
*/
	
	
	
	public static List<Layer> getOrderLayer(List layerList) {
		List<Layer> list = new ArrayList<Layer> ();
		HashMap<String, Layer> layers = new HashMap<String, Layer>();
		
		try {
			for (int i=0;i<layerList.size();i++) {
				Layer layer = (Layer)layerList.get(i);
				if ( layer != null ) {
					if ( layer.getDepth() == 1 )
						layers.put(String.valueOf(layer.getId()),layer);
					else if ( "T".equals(layer.getChecked()) ) {
						Layer temp = (Layer)layers.get(String.valueOf(layer.getPreId())); 
						if ( temp!=null && "T".equals(temp.getChecked()) ){
							layers.put(String.valueOf(layer.getId()),layer);
						}
					}
				}
			}
//			for (int i=1;i<layerList.size();i++) {
//				Layer layer = (Layer)layers.get(String.valueOf(i));
//				if ( layer != null && "T".equals(layer.getChecked())){
//					System.out.println( layer.getId()  + " in return list ");
//					list.add(layer);
//				}
//			}
			Iterator it = layers.keySet().iterator();
			while (it.hasNext()) {
				String key = (String)it.next();
				Layer layer = (Layer)layers.get(key);
				if ( layer != null && "T".equals(layer.getChecked())){
					list.add(layer);
				}
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return list;
	}
	
	public static List getOrederLayer(double scale, List layerList) {
		List list = new ArrayList();
		HashMap layers = new HashMap();
		
		try {
			for (int i=0;i<layerList.size();i++) {
				Layer layer = (Layer)layerList.get(i);
				if ( layer != null ) {
					if ( layer.getDepth() == 1 )
						layers.put(String.valueOf(layer.getId()),layer);
					else if ( "T".equals(layer.getChecked()) ) {
						Layer temp = (Layer)layers.get(String.valueOf(layer.getPreId()));
						if ( "T".equals(temp.getChecked()) && scale < layer.getMaxScale() && scale > layer.getMinScale()  )
							layers.put(String.valueOf(layer.getId()),layer);
					}
				}
			}
			for (int i=1;i<layerList.size();i++) {
				Layer layer = (Layer)layers.get(String.valueOf(i));
				if ( layer != null && "T".equals(layer.getChecked()))
					list.add(layer);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return list;
	}
	
}